package no.weather.widget;

import no.weather.R;
import no.weather.WeatherType;
import no.weather.weatherProvider.StationIsDeletedException;
import no.weather.weatherProvider.WeatherProvider;
import no.weather.weatherProxy.wsKlima.WsKlimaProxy;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class WidgetInitialConfigurationActivity extends Activity {
	private static final String LOG_ID = "no.weather.widget.WidgetInitialConfigurationActivity";
	public static final String PREFS_NAME = "WidgetPreferences";
	private int mAppWidgetId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.widget_init_config);
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		if (extras != null) {
			mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
		}

		// Set result to cancel so if user press back, it returns cancel
		// setResult(RESULT_CANCELED, null);

		Button okButton = (Button) findViewById(R.id.ok);
		okButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sendResult(false);
			}
		});

		Button getNearestStationButton = (Button) findViewById(R.id.widget_init_GetNearestStationButton);
		getNearestStationButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sendResult(true);
			}
		});
	}

	protected void sendResult(boolean getNearestStation) {
		// TODO take care of different providers and make weather type choosable
		String stationNumber;
		String name = "KJELLER";
		String provider = WsKlimaProxy.PROVIDER;
		if (getNearestStation) {
			stationNumber = WidgetProvider.GET_NEAREST_STATION;
		} else {
			EditText stationNumberEditText = (EditText) findViewById(R.id.widgetInitConfigStationNumber);
			stationNumber = stationNumberEditText.getText().toString();
			WeatherProvider weatherProvider = new WeatherProvider(getApplicationContext());
			try {
				name = weatherProvider.getStation(stationNumber, provider, false).getName();
			} catch (StationIsDeletedException e) {
				finish();
			}
		}
		Log.v(LOG_ID, "Station number: " + stationNumber
				+ " is set. Returning...");


		
		WeatherType type[] = new WeatherType[] { new WeatherType("TA", null) };

		// Add settings to db
		new WidgetDatabase(getApplicationContext()).addWidget(mAppWidgetId,
				stationNumber, provider, name, type);

		// Updates widget
		WidgetProvider widgetProvider = new WidgetProvider();
		widgetProvider.onUpdate(this, AppWidgetManager.getInstance(this),
				new int[] { mAppWidgetId });

		// Returns
		Intent resultValue = new Intent();
		resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
		setResult(RESULT_OK, resultValue);
		finish();
	}

}
